<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title borderTop">当前城市</div>
        <div class="button-list">
          <div class="button-wrapper">
            <div class="button">{{this.$store.state.city}}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title borderTop">热门城市</div>
        <div class="button-list">
          <div
            class="button-wrapper"
            v-for="city in hot"
            :key="city.id"
            @click="handleCityClick(city.name)"
          >
            <div class="button">{{city.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(item, key) of cities" :key="key" :ref="key">
        <div class="title borderTop">{{key}}</div>
        <div class="item-list" v-for="city of item" :key="city.id">
          <div class="item" @click="handleMoreCity(city.name)">{{city.name}}</div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Bscroll from 'better-scroll'
export default {
    props:{
        hot: Array,
        cities: Object,
        letter: String
    },
    methods:{
      handleCityClick(city){
        this.$store.dispatch('changeCity', city);
        this.$router.push('/');//这个是跳转的方法
      },
      // handleCityClick(city){
      //   this.$store.commit('changeOnlyCity', city)
      // }这个方法页可以，直接调用action
      handleMoreCity (morecity){
        this.$store.dispatch('changeCity', morecity);
        this.$router.push('/');
      }
    },
    watch:{
        letter(){
            if(this.letter){
                const element = this.$refs[this.letter][0];
                this.scroll.scrollToElement(element)
            }
        }
    },
    mounted(){
        this.scroll = new Bscroll(this.$refs.wrapper)
    },
};
</script>
<style lang="stylus" scoped>
@import '~@/assets/style/varibles.styl';

.list {
  position: absolute;
  top: 1.58rem;
  right: 0;
  left: 0;
  bottom: 0;
  overflow: hidden;

  .title {
    line-height: 0.424rem;
    background: #eee;
    padding-left: 0.2rem;
    font-size: 0.26rem;
    border-bottom: 1px solid #ccc;
  }

  .button-list {
    padding: 0.1rem 0.6rem 0.1rem 0.1rem;
    overflow: hidden;

    .button-wrapper {
      float: left;
      width: 33.3%;

      .button {
        margin: 0.1rem;
        text-align: center;
        border: 0.02rem solid #ccc;
        padding: 0.1rem 0;
        border-radius: 0.06rem;
      }
    }
  }

  .item-list {
    .item {
      line-height: 0.76rem;
      padding-left: 0.2rem;
      border-bottom: 1px solid #ccc;
    }
  }
}
</style>
